import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "@/views/Login";
import Home from "@/components/Home";
import Welcome from "@/views/Welcome";
import User from "@/views/User";
import Role from "@/views/Role";
import Right from "@/views/Right";
import Cate from "@/views/goods/Cate";
import Goods from "@/views/goods/Goods";
import Params from "@/views/goods/Params";
Vue.use(VueRouter)

const routes = [
  {path: '/',redirect:'/login'},
  {path:'/login',component:Login},
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children:[
      {path:'/welcome',component:Welcome},
      {path: '/users',component: User},
      {path: '/roles',component: Role},
      {path: '/rights',component: Right},
      {path: '/categories',component: Cate},
      {path: '/goods',component: Goods},
      {path: '/params',component: Params},
    ]
  },
]

const router = new VueRouter({
  routes
})
//挂载路由导航守卫
router.beforeEach((to, from, next)=>{
  //next() 表示放行  next('/login') 强制放行到某个页面
  //要跳转到登录页 直接放行
  if(to.path==='/login') return next();
  //获取token 如果token不存在 直接跳转到登录页面
  const tokenStr= window.sessionStorage.getItem('token');
  if(!tokenStr) return next('/login');
  //token值存在 直接放行
  next();
})
export default router
